package 设计mos.CompositePattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者：xie
 * @时间：2022/10/11 14:39
 */
public class main {
    public static void main(String[] args) {

        Folder root = new Folder("root");

        AbstractFile folderA = new Folder("folderA");


        File fileB = new File("fileB");

        root.Add(folderA);
        print(root);

    }

    static void print(AbstractFile file){
        file.printName();
        List<AbstractFile> children = file.getChildren();
        for (AbstractFile child: children){
            child.printName();;
        }
    }
}

abstract class AbstractFile{
    protected String name;

    public void printName(){
        System.out.println(name);
    }

    public abstract Boolean Add(AbstractFile abstractFile);
    public abstract Boolean Remove(AbstractFile abstractFile);
    public abstract List<AbstractFile> getChildren();
}

class Folder extends AbstractFile{

    private List<AbstractFile> childrenList = new ArrayList<AbstractFile>();
    public Folder(String name){
        this.name=name;
    }

    @Override
    public Boolean Add(AbstractFile file) {
        return childrenList.add(file);
    }

    @Override
    public Boolean Remove(AbstractFile file) {
        return childrenList.remove(file);
    }

    @Override
    public List<AbstractFile> getChildren() {
        return childrenList;
    }
}

class File extends AbstractFile{
    public File(String name){
        this.name=name;
    }

    @Override
    public Boolean Add(AbstractFile abstractFile) {
        return false;
    }

    @Override
    public Boolean Remove(AbstractFile abstractFile) {
        return false;
    }

    @Override
    public List<AbstractFile> getChildren() {
        return null;
    }
}
